package com.log.common.activities;

import java.util.List;

import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.ext.mobsandgeeks.saripaar.ViewRulePair;
import com.log.common.R;
import com.log.common.util.LogBaseActivity;
import com.log.database.query.LogUserLoginSql;
import com.mobsandgeeks.saripaar.Rule;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.Validator.ValidationListener;
import com.mobsandgeeks.saripaar.annotation.Password;
import com.mobsandgeeks.saripaar.annotation.Required;
import com.mobsandgeeks.saripaar.annotation.SpecialLetterRequired;
import com.mobsandgeeks.saripaar.annotation.TextRule;

public class LogLoginActivity extends LogBaseActivity implements
		ValidationListener, OnFocusChangeListener {

	static final String TAG = LogLoginActivity.class.getSimpleName();

	@Required(order = 1)
	// @Email(order = 2)
	@SpecialLetterRequired(order = 2, message = "Request at least one special character.")
	private EditText emailEditText;

	@Password(order = 3)
	@TextRule(order = 4, minLength = 6, message = "Enter at least 6 characters.")
	private EditText passwordEditText;

	// @ConfirmPassword(order = 5)
	// private EditText confirmPasswordEditText;
	//
	// @Checked(order = 6, message = "You must agree to the terms.")
	// private CheckBox iAgreeCheckBox;

	private Validator validator = null;
	private List<ViewRulePair> failedViewList = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.log_login_scr);

		Button btnSignIn = (Button) findViewById(R.id.sign_in_button);
		btnSignIn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				validator.validateAllControls();
			}
		});

		emailEditText = (EditText) findViewById(R.id.edtUserName);
		emailEditText.setOnFocusChangeListener(this);
		passwordEditText = (EditText) findViewById(R.id.edtPassword);
		passwordEditText.setOnFocusChangeListener(this);

		validator = new Validator(this);
		validator.setValidationListener(this);

	}

	@Override
	public void onValidationSucceeded() {
		// TODO Auto-generated method stub
		// Reset the failed list
		failedViewList = null;

		Toast.makeText(this, "Yay! we got it right!", Toast.LENGTH_SHORT)
				.show();
		LogUserLoginSql user = new LogUserLoginSql(this);
		user.open();
		long result = user.addNewUserAccount("hihi", "hihi");
		Cursor cursor = user.getUserAccount("hihi", "hihi");
		
		user.close();

		Log.i("vulh_" + TAG, "insert: " + result+" - user: "+cursor.getString(0)+" - password: "+cursor.getString(1));
		// Intent logDistributeBudget = new Intent(LogLoginActivity.this,
		// LogDistributeBudget.class);
		// startActivity(logDistributeBudget);
	}

	@Override
	public void onValidationFailed(View failedView, Rule<?> failedRule) {
		// TODO Auto-generated method stub
		String message = failedRule.getFailureMessage();

		if (failedView instanceof EditText) {
			failedView.requestFocus();
			((EditText) failedView).setError(message);
		} else {
			Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onValidationFailedOnFields(
			List<ViewRulePair> viewRulePairCollection) {
		// TODO Auto-generated method stub
		if (viewRulePairCollection == null)
			return;

		failedViewList = viewRulePairCollection;

		for (ViewRulePair failedViewRule : viewRulePairCollection) {
			View failedView = failedViewRule.view;
			String message = failedViewRule.rule.getFailureMessage();

			if (failedView instanceof EditText) {
				((EditText) failedView).setError(message);
				break;
			} else {
				Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
			}
		}

		viewRulePairCollection.get(0).view.requestFocus();
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		// TODO Auto-generated method stub
		Log.i("vulh", "onFocusChange");
		((EditText) v).setError(null);
		if (failedViewList == null) {
			return;
		}

		for (ViewRulePair failedViewRule : failedViewList) {

			View failedView = failedViewRule.view;
			String message = failedViewRule.rule.getFailureMessage();

			if (failedView.getId() == v.getId() && v.hasFocus()) {
				((EditText) v).setError(message);
				Log.i("vulh", message);
			}
		}
	}
}
